<template>
  <div class="app-container">
    <h4 class="form-header h4">当前分组信息</h4>
    <el-form ref="form" :model="form" label-width="80px">
      <el-row>
        <el-col :span="8" :offset="2">
          <el-form-item label="分组编号" prop="attrGroupId">
            <el-input v-model="form.attrGroupId" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="8" :offset="2">
          <el-form-item label="分组名称" prop="attrGroupId">
            <el-input  v-model="form.attrGroupName" disabled />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <h4 class="form-header h4">关联参数信息</h4>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['product:attrattrgroup:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['product:attrattrgroup:remove']"
        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="attrattrgroupList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="分组编号" align="center" prop="attrGroupId" />
      <el-table-column label="属性编号" align="center" prop="attrId" />
      <el-table-column label="分组名称" align="center" prop="attrGroupName" />
      <el-table-column label="属性名称" align="center" prop="attrName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['product:attrattrgroup:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加分组属性关联对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="attrMultiple"
            @click="handleAttr"
            v-hasPermi="['product:attrattrgroup:edit']"
          >关联
          </el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="attrLoading" :data="attrList" @selection-change="handleAttrSelectionChange">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="属性编号" align="center" prop="attrId"/>
        <el-table-column label="属性名" align="center" prop="attrName"/>

        <el-table-column label="可选值列表" align="center" prop="valueSelect">
          <template slot-scope="scope">
            <el-tooltip placement="top">
              <div slot="content">
                <span v-for="(i,index) in scope.row.valueSelect.split(';')" :key="index">{{i}}<br/></span>
              </div>
              <el-tag>{{scope.row.valueSelect.split(";")[0]+" ..."}}</el-tag>
            </el-tooltip>
          </template>
        </el-table-column>

        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">

            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleAttr(scope.row)"
              v-hasPermi="['product:attrattrgroup:edit']"
            >关联
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="attrTotal>0"
        :total="attrTotal"
        :page.sync="attrQueryParams.pageNum"
        :limit.sync="attrQueryParams.pageSize"
        @pagination="getAttrList"
      />
    </el-dialog>

    <el-form label-width="100px">
      <el-form-item style="text-align: center;margin-left:-120px;margin-top:30px;">
        <el-button type="primary" @click="submitForm()">提交</el-button>
        <el-button @click="close()">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {listAttrExcludeHasGroup} from "@/api/product/attr";
import {addAttrattrgroup, addAttrattrgroupList, delAttrattrgroup, listAttrattrgroup} from "@/api/product/attrattrgroup";

export default {
    name: "Attrattrgroup",
    dicts: ['product_attr_type', 'product_disable_normal', 'product_need_search', 'product_yes_no', 'product_value_type'],
    data() {
      return {
        // 属性列表
        attrList:[],
        attrQueryParams: {
          pageNum: 1,
          pageSize: 10,
          attrId: null,
          attrGroupId: null,
          categoryId:undefined
        },
        attrTotal: 0,
        attrLoading: true,
        attrMultiple:true,
        categoryId:undefined,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        attrIdAttrGroupIds:[],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 分组属性关联表格数据
        attrattrgroupList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          attrId: null,
          attrGroupId: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          attrId: [
            { required: true, message: "属性id不能为空", trigger: "blur" }
          ],
          attrGroupId: [
            { required: true, message: "属性分组id不能为空", trigger: "blur" }
          ]
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      handleAttr(row){
        const attrIds = row.attrId || this.ids;

        if (!(attrIds instanceof Array)) {
          this.attrIdAttrGroupIds = [];
          let obj = {};
          obj.attrId = row.attrId;
          obj.attrGroupId = this.form.attrGroupId;
          this.attrIdAttrGroupIds.push(obj);
        }
        let attrIdAttrGroupIds = this.attrIdAttrGroupIds;


        this.$modal.confirm('是否确认关联属性编号为"' + attrIds + '"的数据项？').then(function() {
          return addAttrattrgroupList(attrIdAttrGroupIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("关联成功");
          this.open = false;
        }).catch(() => {});
      },
      handleAttrSelectionChange(selection){
        this.attrIdAttrGroupIds=[];
        selection.forEach(item => {
          let obj = {};
          obj.attrId = item.attrId;
          obj.attrGroupId = this.form.attrGroupId;;
          this.attrIdAttrGroupIds.push(obj);
        });
        this.ids = selection.map(item => item.attrId)
        this.single = selection.length!==1
        this.attrMultiple = !selection.length

      },
      /** 查询分组属性关联列表 */
      getList() {
        this.loading = true;
        // 分组Id
        const attrGroupId = this.$route.params && this.$route.params.attrGroupId;
        // 加入条件
        this.queryParams.attrGroupId=attrGroupId;
        listAttrattrgroup(this.queryParams).then(response => {
          this.form=response.attrGroup;
          this.categoryId=response.attrGroup.categoryId;
          this.attrattrgroupList = response.data.records;
          this.total = response.data.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          attrId: null,
          attrGroupId: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {

        this.attrIdAttrGroupIds=[];
        selection.forEach(item => {
          let obj = {};
          obj.attrId = item.attrId;
          obj.attrGroupId = item.attrGroupId;
          this.attrIdAttrGroupIds.push(obj);
        });
        this.ids = selection.map(item => item.attrId)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      getAttrList(){
        this.attrLoading = true;
        this.attrQueryParams.attrType=1;
        this.attrQueryParams.categoryId=this.categoryId;

        listAttrExcludeHasGroup(this.attrQueryParams).then(response => {
          this.attrList = response.data.records;
          this.attrTotal = response.data.total;
          this.attrLoading = false;
        });
      },
      /** 新增按钮操作 */
      handleAdd() {
        //this.reset();
        this.getAttrList();
        this.open = true;
        this.title = "添加分组属性关联";
      },

      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            addAttrattrgroup(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        });
      },
      /** 关闭按钮 */
      close() {
        const obj = { path: "/product/attr/attrgroup" };
        this.$tab.closeOpenPage(obj);
      },
      /** 删除按钮操作 */
      handleDelete(row) {

        const attrIds = row.attrId || this.ids;

        if (!(attrIds instanceof Array)) {
          this.attrIdAttrGroupIds = [];
          let obj = {};
          obj.attrId = row.attrId;
          obj.attrGroupId = row.attrGroupId;
          this.attrIdAttrGroupIds.push(obj);
        }
        let attrIdAttrGroupIds = this.attrIdAttrGroupIds;


        this.$modal.confirm('是否确认删除关联编号为"' + attrIds + '"的数据项？').then(function() {
          return delAttrattrgroup(attrIdAttrGroupIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      }
    }
  };
</script>
